package com.magic.util;

import org.slf4j.MDC;

import java.io.Serializable;

/**
 * @author magic
 * @version 1.0
 * @date 2022/2/23 10:47
 */
public class  R<T> implements Serializable{
  private static final Integer SUCCESS = 0;
  private static final Integer FAIL = 1;

  private static final long serialVersionUID = 1L;
    private int code;
    private String msg;
    private T data;
    private String traceId;

    public R() {
      this.code = SUCCESS;
      this.msg = "success";
    }

    public R(T data) {
      this.code = SUCCESS;
      this.msg = "success";
      this.data = data;
    }

    public R(T data, String msg) {
      this.code = SUCCESS;
      this.msg = "success";
      this.data = data;
      this.msg = msg;
    }

    public R(Throwable e) {
      this.code = SUCCESS;
      this.msg = "success";
      this.msg = e.getMessage();
      this.code = FAIL;
    }

    public R(int code, String msg, T data) {
      this.code = SUCCESS;
      this.msg = "success";
      this.code = code;
      this.msg = msg;
      this.data = data;
    }

    public R(int code, String msg, T data, String traceId) {
      this.code = SUCCESS;
      this.msg = "success";
      this.code = code;
      this.msg = msg;
      this.data = data;
      this.traceId = traceId;
    }

    public static R ok() {
      return builder().traceId(MDC.get("traceId")).build();
    }

    public static <T> R ok(T data) {
      return builder().data(data).traceId(MDC.get("traceId")).build();
    }

    public static R fail() throws Exception {
      throw new Exception();
    }

    public static R fail(String msg) throws Exception {
      throw new Exception(msg);
    }

    public static <T> R.RBuilder<T> builder() {
      return new R.RBuilder();
    }

    @Override
    public String toString() {
      return "R(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ", traceId=" + this.getTraceId() + ")";
    }

    public int getCode() {
      return this.code;
    }

    public R<T> setCode(int code) {
      this.code = code;
      return this;
    }

    public String getMsg() {
      return this.msg;
    }

    public R<T> setMsg(String msg) {
      this.msg = msg;
      return this;
    }

    public T getData() {
      return this.data;
    }

    public R<T> setData(T data) {
      this.data = data;
      return this;
    }

    public String getTraceId() {
      return this.traceId;
    }

    public R<T> setTraceId(String traceId) {
      this.traceId = traceId;
      return this;
    }

    public static class RBuilder<T> {
      private int code;
      private String msg;
      private T data;
      private String traceId;

      RBuilder() {
      }

      public R.RBuilder<T> code(int code) {
        this.code = code;
        return this;
      }

      public R.RBuilder<T> msg(String msg) {
        this.msg = msg;
        return this;
      }

      public R.RBuilder<T> data(T data) {
        this.data = data;
        return this;
      }

      public R.RBuilder<T> traceId(String traceId) {
        this.traceId = traceId;
        return this;
      }

      public R<T> build() {
        return new R(this.code, this.msg, this.data, this.traceId);
      }

      @Override
      public String toString() {
        return "R.RBuilder(code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + ", traceId=" + this.traceId + ")";
      }
    }

}
